package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.DietPlan;
import com.example.diethealthsystem.service.DietPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dietplans")
public class DietPlanController {

    private final DietPlanService dietPlanService;

    @Autowired
    public DietPlanController(DietPlanService dietPlanService) {
        this.dietPlanService = dietPlanService;
    }

    @GetMapping
    public ResponseEntity<List<DietPlan>> getAllDietPlans() {
        List<DietPlan> dietPlans = dietPlanService.findAll();
        return new ResponseEntity<>(dietPlans, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<DietPlan> getDietPlanById(@PathVariable Long id) {
        return dietPlanService.findById(id)
                .map(dietPlan -> new ResponseEntity<>(dietPlan, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<DietPlan> createDietPlan(@RequestBody DietPlan dietPlan) {
        DietPlan createdDietPlan = dietPlanService.save(dietPlan);
        return new ResponseEntity<>(createdDietPlan, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<DietPlan> updateDietPlan(@PathVariable Long id, @RequestBody DietPlan dietPlan) {
        return dietPlanService.findById(id)
                .map(existingDietPlan -> {
                    existingDietPlan.setPlanName(dietPlan.getPlanName());
                    existingDietPlan.setDescription(dietPlan.getDescription());
                    existingDietPlan.setStartDate(dietPlan.getStartDate());
                    existingDietPlan.setEndDate(dietPlan.getEndDate());
                    existingDietPlan.setUser(dietPlan.getUser()); // 注意：这里可能需要更复杂的逻辑来处理用户关联
                    DietPlan updatedDietPlan = dietPlanService.save(existingDietPlan);
                    return new ResponseEntity<>(updatedDietPlan, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDietPlan(@PathVariable Long id) {
        if (dietPlanService.findById(id).isEmpty()) { // 使用 isEmpty()
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        dietPlanService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<DietPlan>> getDietPlansByUserId(@PathVariable Long userId) {
        List<DietPlan> dietPlans = dietPlanService.findByUserId(userId);
        if (dietPlans.isEmpty()) { // 使用 isEmpty()
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(dietPlans, HttpStatus.OK);
    }
} 